Current Location: Home> Function Categories> is_subclass_of

is_subclass_of

Checks whether the object takes this class as one of its parent classes or implements it: if this object is a subclass of the class, returns true
Name:is_subclass_of
Category:Classes and Objects
Programming Language:php
One-line Description:Check whether an object is a subclass of the specified class

Function name: is_subclass_of()

Applicable version: PHP 4, PHP 5, PHP 7

Function description: The is_subclass_of() function is used to check whether an object is a subclass of the specified class.

Syntax: bool is_subclass_of ( mixed $object , string $class_name [, bool $allow_string = TRUE ] )

parameter:

  • $object: The object to be checked.
  • $class_name: Specify the name of the class.
  • $allow_string (optional): If set to FALSE, the class name must be an object, if set to TRUE (default), the class name can be an object or a string.

Return value: Return TRUE if $object is a subclass or implementation class of $class_name, otherwise FALSE.

Example:

 class ParentClass { } class ChildClass extends ParentClass { } $object = new ChildClass(); // 检查$object 是否是ParentClass 的子类if (is_subclass_of($object, 'ParentClass')) { echo 'ChildClass 是ParentClass 的子类'; } else { echo 'ChildClass 不是ParentClass 的子类'; }

Output:

 ChildClass 是ParentClass 的子类

Notice:

  • If $object is an instance of a class, it is also considered a subclass of that class.
  • If the $allow_string parameter is set to FALSE, $class_name must be an object, otherwise a fatal error will be generated.
  • If $class_name is not a defined class name, a fatal error will be generated.
Similar Functions
Popular Articles